/*
 * Created on Tue May 12 10:05:16 CEST 2009
 */
package se.sweco.coordinator;
import org.osgi.framework.*;

import java.util.HashMap;



public class Activator implements BundleActivator, ServiceListener {
  

	public static BundleContext bc = null;
	public static Coordinator coordinator = null;
	public static HashMap<String, Object> hashen;
	
	
  /* (non-Javadoc)
   * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
   */
  public void start(BundleContext context) throws Exception {
	  	Activator.bc = context;
	  	HashMap<String, Object> hm = new HashMap<String, Object>();
	  	Activator.hashen = hm;
	  	context.addServiceListener(this);
	  	coordinator = new Coordinator(0,2000);
	  	getActiveServices(context);
	  	
	  	System.out.println("Bundle Started");
  }

  
  public void getActiveServices(BundleContext context) {
	  ServiceReference ref;
	  ref = context.getServiceReference("se.sweco.gui.TileMap");
	  if(ref != null){
		  hashen.put("se.sweco.gui.TileMap",coordinator.startGUIService());
	  }
	  ref = context.getServiceReference("se.sweco.dataservice.TaxDataService");
	  if(ref != null){
		  hashen.put("se.sweco.dataservice.TaxDataService",coordinator.startTaxService());
	  }
	  ref = context.getServiceReference("se.sweco.dataservice.TrafficDataService");
	  if(ref != null){
		  hashen.put("se.sweco.dataservice.TrafficDataService",coordinator.startTrafficService());
	  }
	  ref = context.getServiceReference("se.sweco.gpsservice.GPSConnect");
	  if(ref != null){
		  hashen.put("se.sweco.gpsservice.GPSConnect",coordinator.startGPSService());
	  }
  }
  
  /* (non-Javadoc)
   * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
   */
  public void stop(BundleContext context) throws Exception {
	  	System.out.println("Bundle Stopped");
	  	coordinator.timer.cancel();
		Activator.bc = null;
  }
  
  public void serviceChanged(ServiceEvent event)
	{
	  	String[] objectClass = (String[]) 
	  	event.getServiceReference().getProperty("objectClass");
	  	System.out.println(objectClass.length);
	  	
	  	for(int i=0;i<objectClass.length;i++){
	  		
	  		switch (event.getType()) {
	  			case ServiceEvent.REGISTERED:
	  				
	  				System.out.println("reggad");
	  				System.out.println(objectClass[i]);
	  				if(objectClass[i].equals("se.sweco.gui.TileMap")){
	  					hashen.put(objectClass[i], coordinator.startGUIService());
	  				}
	  				else if(objectClass[i].equals("se.sweco.dataservice.TaxDataService")){
	  					hashen.put(objectClass[i],coordinator.startTaxService());
					
	  				}
	  				else if(objectClass[i].equals("se.sweco.dataservice.TrafficDataService")){
	  					hashen.put(objectClass[i],coordinator.startTrafficService());
	  				}
	  				else if(objectClass[i].equals("se.sweco.gpsservice.GPSConnect")){
	  					hashen.put(objectClass[i],coordinator.startGPSService());
	  				}
	  				break;
	  			case ServiceEvent.MODIFIED:
	  				/*coordinator.timer.cancel();
	  				coordinator = new Coordinator(0,5000);
	  				*/
	  				break;
	  			case ServiceEvent.UNREGISTERING:
	  				hashen.remove(objectClass[i]);
	  				System.out.println("unreg");
	  				break;
		}
	  }
		
	}

}